<?php


namespace App\Http\Controllers\Weapp;


use App\Tool\Json;
use Illuminate\Support\Facades\Cache;

class Store extends Auth
{


    function getStoreList()
    {


        $user_id = resolve('weapp_user')['id'];

        $list = \App\Model\Store::orderBy('id', 'desc')->get();

        if (!$list) return Json::code(1, 'success');

        $store_id = Cache::get('store:default_user_id_' . $user_id);

        foreach ($list as $key => $value) {

            if ($store_id == $value->id) {

                $list[$key]->is_default = true;

            } else {

                $list[$key]->is_default = false;
            }

        }


        return Json::code(1, 'success', $list);
    }


    function setDefaultStore()
    {

        $user_id = resolve('weapp_user')['id'];

        $id = request()->input('id');

        Cache::put('store:default_user_id_' . $user_id, $id);

        return Json::code(1, 'success');

    }

    function getDefaultStore()
    {

        $user_id = resolve('weapp_user')['id'];

        $store_id = Cache::get('store:default_user_id_' . $user_id);

        $info = \App\Model\Store::orderBy('id', 'desc')->first();

        if (!$store_id) {


            return Json::code(1, 'success', $info);

        } else {

            $find_store = \App\Model\Store::find($store_id);

            $find_store->is_default=true;

            if ($find_store) return Json::code(1, 'success', $find_store);


            return Json::code(1, 'success', $info);
        }


    }


}
